package logic

import (
	"context"
	"develop/model/mysql"
	"develop/usersrpc/internal/svc"
	"develop/usersrpc/usersrpc"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectAddLogic {
	return &CollectAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectAddLogic) CollectAdd(in *usersrpc.CollectAddRequest) (*usersrpc.CollectAddResponse, error) {
	// todo: add your logic here and delete this line
	var collectId mysql.Collection_Info
	err := collectId.FindCollectId(mysql.DB, in.UserId, in.VideoId)
	if err != nil {
		return nil, err
	}
	if collectId.ID != 0 {
		return nil, errors.New("已收藏该视频")
	}
	collectionInfo := &mysql.Collection_Info{
		Video_id: int(in.VideoId),
		User_id:  int(in.UserId),
	}
	err = collectionInfo.CollectAdd(mysql.DB)
	if err != nil {
		return nil, err
	}
	return &usersrpc.CollectAddResponse{
		CollectId: int64(collectionInfo.ID),
	}, nil
}
